package org.mozhu.mboot.core.web.interceptor;

import org.mozhu.mboot.core.web.HttpServletRequestSkipper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

public abstract class BaseHandlerInterceptorAdapter extends HandlerInterceptorAdapter {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	private Set<String> exclusions;
	private Set<String> inclusions;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpServletRequestSkipper skipper = new HttpServletRequestSkipper(exclusions, inclusions);
		if (skipper.needSkip(request)) {
			logger.debug("Skip SevletPath [{}]", request.getServletPath());
			return true;
		}
		return doPreHandle(request, response, handler);
	}

	protected abstract boolean doPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception;

	public void setExclusions(Set<String> exclusions) {
		this.exclusions = exclusions;
	}

	public void setInclusions(Set<String> inclusions) {
		this.inclusions = inclusions;
	}

}
